using System;
using System.Xml;
using System.Xml.Serialization;

namespace Core.Graphics
{
    [Serializable()]
    [XmlRoot("TileID")]
    public class TileID
    {
        [XmlAttribute("ID")]
        public int ID { get { return _ID; } set { _ID = value; } }
        [XmlAttribute("TileSetID")]
        public int TileSetID { get { return _TileSetID; } set { _TileSetID = value; } }

        private int _ID;
        private int _TileSetID;

        public TileID(int ID, int TileSetID)
        {
            _ID = ID;
            _TileSetID = TileSetID;
        }

        public TileID(int ID, TileSet TileSet)
        {
            _ID = ID;
            if (TileSet == null)
            {
                _TileSetID = -1;
            }
            else
                _TileSetID = TileSet.ID;
        }

        public TileID() { /*For XML Serialization */ }

        ///<summary>Represents an Empty Sprite</summary>
        public static TileID EmptyID  { get { return _EmptyID; } }
        private static TileID _EmptyID = new TileID(-1, -1);

        ///<summary>Represents an Empty Sprite</summary>
        public static TileID NonExistantID { get { return _NonExistantID; } }
        private static TileID _NonExistantID = new TileID(-2, -2);

        public static bool operator ==(TileID x, TileID y)
        {
            if (object.Equals(x, null) && !object.Equals(y, null) || object.Equals(y, null) && !object.Equals(x, null))
                return false;

            //If they have the same Parent TileSet
            if (x.TileSetID == y.TileSetID)
            {
                //Then compare the values and return compare result.
                return x.ID == y.ID;
            }

            return false;
        }

        public static bool operator !=(TileID x, TileID y)
        {
            return x._ID != y._ID || x._TileSetID != y._TileSetID;
        }

        public override int GetHashCode()
        {
            return _ID.GetHashCode() + _TileSetID.GetHashCode();
        }
    }
}
